package com.qdxwx.web.controller.subject;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.data.service.advert.AdvertService;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookQuery;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureQuery;
import com.qdxwx.models.subject.Subject;
import com.qdxwx.models.advert.Advert;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 首页专业分类
 */
@Controller
@RequestMapping("/subject")
public class SubjectIndexController extends BaseController {

    @Autowired
    private SubjectService subjectService;
    @Autowired
    private BookService bookService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AdvertService advertService;

    @GetMapping("/{id:[0-9]+}.html")
    public String index(HttpServletRequest request, @PathVariable("id") int id, ModelMap model) {
        Subject subject = subjectService.get(id);
        if (subject == null) return toError404(request);
        model.put("subject", subject);
        Category category = new Category();
        category.setSubjectId(subject.getSubjectId());
        category.setParentId(0);
        category.setChannelId(2);
        List<Category> equalCategorys = categoryService.getList(category);
        model.put("equalCategorys", equalCategorys);
        category.setChannelId(8);
        List<Category> proCategorys = categoryService.getList(category);
        model.put("proCategorys", proCategorys);
        category.setChannelId(10);
        List<Category> pictureCategorys = categoryService.getList(category);
        model.put("pictureCategorys", pictureCategorys);
        category.setChannelId(9);
        List<Category> bookCategorys = categoryService.getList(category);
        model.put("bookCategorys", bookCategorys);
        //banner图
        List<Advert> advertList = (List<Advert>) CacheUtils.get(CacheConstants.ADVERT_LIST + ":subject:" + id);
        if (advertList == null || advertList.size() < 1) {
            Advert imager = new Advert();
            imager.setTypeId(1);
            advertList = advertService.getList(imager, null);
            if (advertList != null && advertList.size() > 0) {
                CacheUtils.set(CacheConstants.ADVERT_LIST + ":subject:" + id, advertList, CacheConstants.BANNER_IMAGES_TIME);
            }
        }
        model.put("advertList", advertList);

        //同步课
        List<Course> equalCourses = (List<Course>) CacheUtils.get(CacheConstants.NAME_SPACE +  ":subject:equalCourses:" + id);
        if (equalCourses == null || equalCourses.size() < 1) {
            CourseQuery courseQuery = new CourseQuery();
            courseQuery.setChannelId(2);
            courseQuery.setSubjectId(subject.getSubjectId());
            courseQuery.setIsAvailable(1);
            equalCourses = courseService.getList(courseQuery, new PageRequest(1, 6));
            if (equalCourses.size() > 0) {
                CacheUtils.set("qdxwx:subject:equalCourses:" + id, equalCourses, CacheConstants.WEB_STATISTICS_TIME);
            }
        }
        model.put("equalCourses", equalCourses);
        //大师指路
        List<Course> proCourses = (List<Course>) CacheUtils.get("qdxwx:subject:proCourses:" + id);
        if (proCourses == null || proCourses.size() < 1) {
            CourseQuery courseQuery = new CourseQuery();
            courseQuery.setChannelId(8);
            courseQuery.setSubjectId(subject.getSubjectId());
            courseQuery.setIsAvailable(1);
            proCourses = courseService.getList(courseQuery, new PageRequest(1, 6));
            if (proCourses.size() > 0) {
                CacheUtils.set("qdxwx:subject:proCourses:" + id, proCourses, CacheConstants.WEB_STATISTICS_TIME);
            }
        }
        model.put("proCourses", proCourses);
        //素材
        List<Picture> pictures = (List<Picture>) CacheUtils.get("qdxwx:subject:pictures:" + id);
        if (pictures == null || pictures.size() < 1) {
            PictureQuery pictureQuery = new PictureQuery();
            pictureQuery.setSubjectId(subject.getSubjectId());
            pictureQuery.setStatus(1);
            pictureQuery.setOrderBy(3);
            pictures = pictureService.getList(pictureQuery, new PageRequest(1, 8));
            if (pictures.size() > 0) {
                CacheUtils.set("qdxwx:subject:pictures:" + id, pictures, CacheConstants.WEB_STATISTICS_TIME);
            }
        }
        model.put("pictures", pictures);
        //书籍
        List<Book> books = (List<Book>) CacheUtils.get("qdxwx:subject:books:" + id);
        if (books == null || books.size() < 1) {
            BookQuery bookQuery = new BookQuery();
            bookQuery.setSubjectId(subject.getSubjectId());
            bookQuery.setIsAvailable(1);
            books = bookService.getList(bookQuery, new PageRequest(1, 6));
            if (books.size() > 0) {
                CacheUtils.set("qdxwx:subject:books:" + id, books, CacheConstants.WEB_STATISTICS_TIME);
            }
        }
        model.put("books", books);
        return getViewPath("subject/subject");
    }

}
